import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class gmmStrat extends JFrame{
	BufferedImage bg, player, mp;
	gmmStrat gm;
	int xcoord, ycoord;
	Color red = new Color(.9f, .1f, .1f, .7f);




	public gmmStrat(){
		try {
			mp = ImageIO.read(new File("Levels/moving_h.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.exit(0);
			e.printStackTrace();
		}
		
		setTitle("strat");
		gm = this;
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(1280, 800);
		setVisible(true);

		this.createBufferStrategy(2);

		Thread thrd = new Thread(new FrameDrawer() );
		thrd.start();




	}

	class FrameDrawer implements Runnable{

		int targetMsPerFrame = 16;
		long lastFrameTimeStamp = 0;

		public void run() {
			
			while(true){
				long nt = System.nanoTime();
				lastFrameTimeStamp =  System.currentTimeMillis();
				xcoord +=2;
				xcoord%=1280;
				drawStuff();
				System.out.println(System.nanoTime() - nt);

				try {
					long slp = (long)16 - (System.currentTimeMillis() - lastFrameTimeStamp);
					if(slp >=0){
//						System.out.println("slp:"+slp);
						Thread.sleep(slp);
					}else{
						//Uh oh, frames take longer to draw than our dT allows... aka can't keep up the framerate
					}
				} catch (InterruptedException e) {/*shit*/}
				
				
			}
		}

		private void drawStuff() {
//			System.out.println("drawStuff");
			BufferStrategy bf = gm.getBufferStrategy();
			Graphics g = null;
			if(bf!=null){
				g = bf.getDrawGraphics();	 
				System.out.println("lolface");


				g.setColor(red);
//				g.drawRect(xcoord, ycoord, 160, 40);
				
				g.drawImage(mp,xcoord,ycoord,null);
				
				
				g.dispose();
				bf.show();
				

			}
			// It is best to dispose() a Graphics object when done with it.

			// Shows the contents of the backbuffer on the screen.

			//Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
			//Drawing is done which looks very jerky
			Toolkit.getDefaultToolkit().sync();	
		}




	}


	public static void main(String[]args) {	new gmmStrat();}
}

